package com.cskaoyan.service.impl;

import com.cskaoyan.bean.AddressParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.model.MarketAddress;
import com.cskaoyan.model.MarketAddressExample;
import com.cskaoyan.service.AddressService;
import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AddressServiceImpl implements AddressService {


    @Override
    public PageData list(AddressParameter addressParameter) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketAddressMapper mapper = sqlsession.getMapper(MarketAddressMapper.class);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        //分页
        Integer page = addressParameter.getPage();
        Integer limit = addressParameter.getLimit();
        PageHelper.startPage(page, limit);
        //排序
        String sort = addressParameter.getSort();
        String order = addressParameter.getOrder();
        marketAddressExample.setOrderByClause(sort + " " + order);
        //条件
        Integer userId = addressParameter.getUserId();
        if (userId != null && userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        String name = addressParameter.getName();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketAddress> marketAddresses = mapper.selectByExample(marketAddressExample);

        sqlsession.commit();
        sqlsession.close();
        return PageData.list(marketAddresses);
    }
}
